package utils;

import javax.swing.JFileChooser;

/**
 *
 * Class used to load and save files, is responsible for deploying a new view with the load or save parameters respectively.
 * 
 * @author Andy Cullache
 */
public class JFileChooserClass {

	/**
	 * Select the name and directory of the file to save.
	 * 
	 * @param filterName      Filter to files in diretory.
	 * @param filterExtension      Extension to filtername.
	 * @param title      Title of view which sets the parameters.
	 * @return      Array with the name (value [o]) and the file directory (value [1]).
	 */
	public static String[] selectFileDirectoryToSave(String filterName, String filterExtension, String title) {

        String file = "";
        String path = "";
        String FilePath[] = new String[2];

        String Directorio = "Mis documentos";
        JFileChooser selectDir = new JFileChooser(Directorio);
        selectDir.setFileSelectionMode(JFileChooser.FILES_ONLY);
        selectDir.setDialogTitle(title);

        Filter filtro = new Filter(filterName);
        filtro.addExtension(filterExtension);
        selectDir.setFileFilter(filtro);
        int returnVal = selectDir.showDialog(null, "OK");

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = selectDir.getSelectedFile().getName();
            path = selectDir.getSelectedFile().getParent();
            FilePath[0] = file;
            FilePath[1] = path;
        }
        return FilePath;
    }
	
	/**
	 * Select the file to load.
	 * 
	 * @return      Array with the file name (value [o]) and the file directory (value [1]).
	 */
	public static String[] selectFileToRead() {

        String file = "";
        String path = "";
        String FilePath[] = new String[2];

        String Directorio = "Mis documentos";
        JFileChooser selectDir = new JFileChooser(Directorio);
        selectDir.setFileSelectionMode(JFileChooser.FILES_ONLY);
        selectDir.setDialogTitle("Load Test: Select directory");

        Filter filtro = new Filter("RBox Result file");
        filtro.addExtension(".csv");
        selectDir.setFileFilter(filtro);
        int returnVal = selectDir.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = selectDir.getSelectedFile().getName();
            path = selectDir.getSelectedFile().getParent();
            FilePath[0] = file;
            FilePath[1] = path;
        }
        return FilePath;
    }
	
}